package com.st.event.demo.strategy;

import com.st.event.demo.post.OrderFinishTriggerListener;
import com.st.event.demo.source.EventAttribute;

import java.lang.reflect.Method;
import java.util.Map;

public interface StrategyHandler {

    /**
     * 执行前置处理
     *
     * @param attribute
     * @param clientInterface
     * @param method
     * @param args
     * @return
     */
    default Object beforeExecute(EventAttribute attribute, Class<?> clientInterface, Method method, Object[] args) {
        return null;
    }

    /**
     * 执行
     *
     * @param attribute
     * @param params
     * @return
     * @throws Exception
     */
    Object execute(EventAttribute attribute, Object... params) throws Exception;

    /**
     * 执行后置处理
     *
     * @param attribute
     * @param clientInterface
     * @param method
     * @param args
     * @return
     */
    default Object afterExecute(EventAttribute attribute, Class<?> clientInterface, Method method, Object[] args, Map<String, OrderFinishTriggerListener> params) {
        // 执行的ben
        String beanName = attribute.beanName();
        // 执行的方法
        String actionMethodName = attribute.actionMethodName();
        if (params.containsKey(beanName)) {
            OrderFinishTriggerListener triggerListener = params.get(beanName);
            // todo:执行的方法名和方法匹配
            triggerListener.process(args[0].toString());
        }
        return null;
    }

    default Object exception(EventAttribute attribute, Class<?> clientInterface, Method method, Object[] args, Exception ex) {
        System.out.println("异常处理......");
        return null;
    }

    boolean apply(EventAttribute eventAttribute);

}
